package cn.jekun.shop.web.action;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.bean.SessionUser;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.pay.base.BasePay;
import cn.jekun.shop.common.service.ShopBaseAction;
import cn.jekun.shop.web.bean.MemberSession;
import cn.jekun.shop.web.service.UserService;

public class UserAction extends ShopBaseAction{
	//记录日志信息
		private static Logger logger=Logger.getLogger(BasePay.class);
			
	@Resource(name=UserService.BEAN_NAME)
	UserService userService;
	
	
	/**
	 * 获取个人的统计信息(
	 * 		积分：5620 
			待收货订单：1份 
			待付款订单：2份 
			已成功订单：)
	 * @return
	 */
	public String getStatInfo(){
		JSONObject jsonObject = null;
		//验证是否登录
		if(!isLogin()){
			return timeout();
		}
		try{
			String userId = this.getUserId();
			jsonObject = userService.getStatInfo(userId);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 动态获取用户基本信息
	 * @return
	 */
	public String getUserInfo(){
		JSONObject jsonObject = null;
		//验证是否登录
		if(!isLogin()){
			return timeout();
		}
		try{
			String userId = this.request.getParameter("userId");
			jsonObject = userService.getUserInfo(userId);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 获取用户基本信息
	 * @return
	 */
	public String getUserInfoByDynamic(){
		
		logger.info("********************   UserAction getUserInfoByDynamic  begin ******************** ");
		JSONObject jsonObject = null;
		
		MemberSession memberSession=(MemberSession) this.request.getSession().getAttribute(SessionUser.SESSION_KEY);
		try{
			
			if(memberSession!=null){
				logger.info("********************  用户已登录 ******************** ");
				String userId=memberSession.getUserId();
				if(userId!=null){
					jsonObject = userService.getUserInfo(userId);
				}
			}else{
				
				logger.info("********************  用户未登录 ******************** ");
			}
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 更新用户基本信息
	 * @return
	 */
	public String updateUserInfo(){
		JSONObject jsonObject = null;
		//验证是否登录
		if(!isLogin()){
			return timeout();
		}
		try{
			String userId = this.getUserId();
			String userName = this.request.getParameter("userName");
			String mobile = this.request.getParameter("mobile");
			String sex = this.request.getParameter("sex");
			String birthday = this.request.getParameter("birthday");
			String phone = this.request.getParameter("phoneNumber");
			String email = this.request.getParameter("email");
			String address = this.request.getParameter("address");
			String zipCode = this.request.getParameter("zipcode");
			String realName = this.request.getParameter("realName");
			String iconUrl = this.request.getParameter("iconUrl");
			jsonObject = userService.updateUserInfo(userId,userName,mobile,sex,birthday,phone,email,address,zipCode,realName,iconUrl);
			
		
			if(!StringUtils.isBlank(iconUrl)){
				
				MemberSession memberSession = (MemberSession) this.request.getSession().getAttribute(MemberSession.SESSION_KEY);

				if(!memberSession.getIconUrl().equals(iconUrl)){
					
					memberSession.setIconUrl(iconUrl);

					this.request.getSession().setAttribute(MemberSession.SESSION_KEY, memberSession);

				}

			}
		
			
			
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "更新用户基本信息成功,来源:web,用户代码:"+this.getUserId());//记录操作日志

		} catch (BusinessException e) {
			
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
				this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "更新用户基本信息失败,原因:"+jsonObject.get("message")+",来源:web,用户代码:"+this.getUserId());//记录操作日志

		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		response.setStatus(HttpServletResponse.SC_OK);
		response.setContentType("text/html");

		return this.writeJson(jsonObject);
		
	}

}
